package com.shijiu.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shijiu.annotation.RepeatSubmit;
import com.shijiu.dao.mapper.UserMapper;
import com.shijiu.pojo.Shop;
import com.shijiu.pojo.User;
import com.shijiu.service.CommentService;
import com.shijiu.service.ShopService;
import com.shijiu.service.UserService;
import com.shijiu.vo.PageParams;
import com.shijiu.common.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;
import java.util.Optional;

/**
 * @Author xlj
 * @Date Created in  2023/3/18 14:30
 * @DESCRIPTION:
 * @Version V1.0
 */
@RestController
@RequestMapping("admin")
@Transactional(rollbackFor = Exception.class)
public class AdminController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;
    @Autowired
    private ShopService shopService;

    @ApiOperation("管理员展示所有评论")
    @GetMapping("commentList")
    public Result commentList() {
        return Result.success(commentService.list());
    }
    @ApiOperation("管理员分页展示所有评论")
    @PostMapping("commentListByPage")
    public Result commentListByPage(@RequestBody Map<String, Object> requestBodyMaps) {
        return commentService.queryCommentList(requestBodyMaps);
    }
    @ApiOperation("根据评论id查询评论")
    @GetMapping("queryComment/{id}")
    public Result queryById(@PathVariable("id") Integer id) {
        return Result.success((commentService.getById(id)));
    }

    @ApiOperation("根据评论id删除评论")
    @GetMapping("removeComment/{id}")
    public Result removeComment(@PathVariable("id") Integer id) {
        return Result.success(commentService.removeById(id));
    }
    @ApiOperation("查询用户列表 账号密码")
    @GetMapping("userList")
    public Result userList(){
        return Result.success(userService.list());
    }
    @ApiOperation("分页查询用户列表 账号密码")
    @PostMapping("userListByPage")
    public Result userListByPage(@RequestBody PageParams pageParams){
        return userService.queryUserList(pageParams);
    }
    @ApiOperation("删除店主信息")
    @PostMapping("deleteUserById")
    @RepeatSubmit(interval = 2000, message = "请求过于频繁")
    public Result deleteUserById(@RequestBody @Validated  User user){
        Optional<Integer> idOptional = Optional.ofNullable(user.getId());
        if (!idOptional.isPresent()) {
            // user.getId() is null
            throw new RuntimeException("用户id为空");
        }
        return Result.success(userService.removeById(user.getId()));
    }
    @ApiOperation("新增用户")
    @PostMapping("saveUser")
    @RepeatSubmit(interval = 2000, message = "接口请求过于频繁")
    public Result saveUser(@RequestBody User user){
        Date date = new Date();
        user.setDisable("0");
        user.setIsAdmin(false);
        String phone = user.getPhone();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone,phone);
        User isUser = userMapper.selectOne(wrapper);
        if (isUser != null) {
            return Result.fail(500,"注册失败,用户手机号重复");
        }
        LambdaQueryWrapper<User> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(User::getEmail,user.getEmail());
        User selectOne = userMapper.selectOne(wrapper1);
        if (selectOne != null) {
            return Result.fail(500,"注册失败,用户邮箱账号重复");
        }
        User userModel = new User();
        BeanUtils.copyProperties(user,userModel);
        userModel.setCreateTime(date);
        return Result.success(userService.save(userModel));
    }
    @ApiOperation("是否禁用用户")
    @PostMapping("disableUser")
    @RepeatSubmit(interval = 2000, message = "接口请求过于频繁")
    public Result disableUser(@RequestBody @Validated User user){
        Integer userId = user.getId();
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId,userId);
        User userMessage = userService.getOne(wrapper);
        String disable = userMessage.getDisable();
        User users = new User();
        users.setId(userId);
        if (StrUtil.isNotBlank(disable)) {
           if ("0".equals(disable)){
               users.setDisable("1");
           }else {
               users.setDisable("0");
           }
        }
        return Result.success(userService.updateById(users));
    }
    @ApiOperation("修改用户名密码")
    @RepeatSubmit(interval = 2000, message = "接口请求过于频繁")
    @PostMapping("updateUser")
    public Result updateUser(@RequestBody User user){
        return Result.success(userService.updateById(user));
    }
    @ApiOperation("查询所有店铺-分页")
    @PostMapping("shopListByPage")
    public Result shopList(@RequestBody PageParams pageParams){
        return shopService.queryShopList(pageParams);
    }
    @ApiOperation("新增店铺")
    @RepeatSubmit(interval = 2000, message = "接口请求过于频繁")
    @PostMapping("saveShop")
    public Result saveShop(@RequestBody Shop shop){
        return shopService.saveShop(shop);
    }
    @ApiOperation("更新店铺")
    @RepeatSubmit(interval = 2000, message = "接口请求过于频繁")
    @PostMapping("updateShop")
    public Result updateShop(@RequestBody Shop shop){
        return Result.success(shopService.updateById(shop));
    }
    @ApiOperation("删除店铺")
    @RepeatSubmit(interval = 2000, message = "接口请求过于频繁")
    @PostMapping("deleteShopById")
    public Result deleteShopById(@RequestBody Shop shop){
        Optional<Integer> idOptional = Optional.ofNullable(shop.getId());
        if (!idOptional.isPresent()) {
            // user.getId() is null
            throw new RuntimeException("店铺id为空");
        }
        return Result.success(shopService.removeById(shop.getId()));
    }

}
